<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      size="small"
      @keyup.enter.native="getDataList()"
      label-width="120"
    >
      <el-form-item label="仓库">
        <el-select v-model="dataForm.wareId" placeholder="请选择仓库" clearable>
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in wareList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="采购单">
        <el-select
          v-model="dataForm.purchaseId"
          placeholder="请选择采购单"
          clearable
        >
          <el-option
            :label="w.purchaseName"
            :value="w.id"
            v-for="w in purchaseList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="dataForm.status" placeholder="请选择状态" clearable>
          <el-option label="新建" :value="0"></el-option>
          <el-option label="已分配" :value="1"></el-option>
          <el-option label="正在采购" :value="2"></el-option>
          <el-option label="已完成" :value="3"></el-option>
          <el-option label="采购失败" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-dropdown
          @command="handleBatchCommand"
          :disabled="dataListSelections.length <= 0"
        >
          <el-button type="danger">
            批量操作
            <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="delete">批量删除</el-dropdown-item>
            <el-dropdown-item command="merge">合并整单</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column type="index" align="center" label="编号" width="80" />

      <el-table-column
        prop="purchaseName"
        header-align="center"
        align="center"
        label="采购单名称"
      ></el-table-column>

      <el-table-column
        prop="skuId"
        header-align="center"
        align="center"
        label="采购商品id"
      ></el-table-column>
      <el-table-column
        prop="skuNum"
        header-align="center"
        align="center"
        label="采购数量"
      ></el-table-column>
      <el-table-column
        prop="skuPrice"
        header-align="center"
        align="center"
        label="采购金额"
      ></el-table-column>
      <el-table-column
        prop="wareName"
        header-align="center"
        align="center"
        label="仓库名称"
      ></el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0">新建</el-tag>
          <el-tag type="info" v-if="scope.row.status == 1">已分配</el-tag>
          <el-tag type="wanring" v-if="scope.row.status == 2">正在采购</el-tag>
          <el-tag type="success" v-if="scope.row.status == 3">已完成</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 4">采购失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <el-dialog title="合并到整单" :visible.sync="mergedialogVisible">
      <el-form>
        <el-form-item>
          <el-select
            style="width: 100%"
            v-model="purchaseId"
            placeholder="请选择"
            @filter-method="purchaseSearch"
            clearable
          >
            <el-option
              v-for="item in purchasetableData"
              :key="item.id"
              :label="item.purchaseName"
              :value="item.id"
            >
              <span style="float: left; color: #8492a6; font-size: 13px"
                >{{ item.purchaseName }}:{{ item.assigneeName }}：{{
                  item.phone
                }}</span
              >
              <span style="float: right">{{ item.wareName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="mergedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="batchMergeItem">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./purchasedetail-add-or-update";
import {
  wareInfoList,
  warePurchaseList,
  warePurchaseDeatailPageList,
  warePurchaseDeatailRemove,
  warePurchaseUnreceive,
  warePurchaseMerge,
} from "@/api/ware";
export default {
  data() {
    return {
      dataForm: {
        status: "",
        wareId: "",
        purchaseId: "",
      },
      wareList: [],
      purchaseList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      mergedialogVisible: false,
      purchaseId: "",
      purchasetableData: [],
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
    this.getWares();
    this.getPurchases();
    this.getUnreceivedPurchase();
  },
  methods: {
    purchaseSearch(search) {
      debugger;
      console.log(search, "-----------");
      this.purchasetableData = this.purchasetableData.filter((item) => {
        return (
          item.assigneeName.indexOf(search) > -1 ||
          item.phone.indexOf(search) > -1
        );
      });
    },
    batchMergeItem() {
      let items = this.dataListSelections.map((item) => {
        return item.id;
      });
      if (!this.purchaseId) {
        this.$confirm(
          "没有选择任何【采购单】，将自动创建新单进行合并。确认吗？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            warePurchaseMerge({ items: items }).then((res) => {
              if (res && res.code === 0) {
                this.getDataList();
              }
            });
          })
          .catch(() => {});
      } else {
        warePurchaseMerge({ purchaseId: this.purchaseId, items: items }).then(
          (res) => {
            if (res && res.code === 0) {
              this.getDataList();
            }
          }
        );
      }
      this.mergedialogVisible = false;
    },
    getUnreceivedPurchase() {
      warePurchaseUnreceive().then((res) => {
        if (res && res.code === 0) {
          this.purchasetableData = res.data;
        } else {
          this.purchasetableData = [];
        }
      });
    },
    handleBatchCommand(cmd) {
      if (cmd == "delete") {
        if (this.dataListSelections.length <= 0) {
          this.$message({
            message: "请先选择要删除的数据",
            type: 'warning',
            duration: 1500,
            onClose: () => {},
          });
        } else {
          this.deleteHandle();
        }
      }
      if (cmd == "merge") {
        if (this.dataListSelections.length != 0) {
          this.mergedialogVisible = true;
        } else {
          this.$alert("请先选择需要合并的需求", "提示", {
            confirmButtonText: "确定",
            type: "warning",
            callback: (action) => {},
          });
        }
      }
    },
    getWares() {
      wareInfoList().then((res) => {
        if (res && res.code === 0) {
          this.wareList = res.data;
        } else {
          this.wareList = [];
        }
      });
    },
    getPurchases() {
      warePurchaseList().then((res) => {
        if (res && res.code === 0) {
          this.purchaseList = res.data;
        } else {
          this.purchaseList = [];
        }
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      warePurchaseDeatailPageList({
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
        status: this.dataForm.status,
        wareId: this.dataForm.wareId,
        purchaseId: this.dataForm.purchaseId,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = res.data.data;
          this.totalPage = res.data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定是否进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        warePurchaseDeatailRemove({ ids: ids }).then((res) => {
          if (res && res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
  },
};
</script>
